from BloodSpiderAPI import models

from BloodSpiderModel.DjangoResponseTool import response_dict

from django import forms

from BloodSpiderAPI.apis.feedback.utils import utils_model_to_dict

from django.db.models import Q


# 反馈表单
class FeedbackForm(forms.ModelForm):
    class Meta:
        model = models.Feedback
        fields = ['feedback_type', 'content']


# 创建反馈
def create_feedback(request):
    if request.method != "POST":
        return response_dict.response_dict(1, "请求方法错误", "仅支持POST方法")
    try:
        form = FeedbackForm(request.POST)
        if form.is_valid():
            feedback = form.save(commit=False)
            feedback.user = request.user
            feedback.save()
            data = utils_model_to_dict(feedback)
            return response_dict.response_dict(0, "反馈创建成功", data)
        else:
            err = response_dict.get_first_error(form)
            return response_dict.response_dict(1, "反馈创建失败", err)
    except Exception as e:
        return response_dict.response_dict(1, "反馈创建失败", str(e))


# 上传反馈图片
def upload_feedback_image(request):
    if request.method != "POST":
        return response_dict.response_dict(1, "请求方法错误", "仅支持POST方法")
    try:
        if "image" not in request.FILES:
            return response_dict.response_dict(1, "上传失败", "未包含图片文件")
        image = request.FILES["image"]
        photo_type = request.POST.get("photo_type", 1)
        if not photo_type:
            return response_dict.response_dict(1, "上传失败", "未包含图片类型")
        elif int(photo_type) not in [1, 2]:
            return response_dict.response_dict(1, "上传失败", "图片类型错误")
        feedback_id = request.POST.get("feedback_id")
        if not feedback_id:
            return response_dict.response_dict(1, "上传失败", "未包含反馈ID")
        feedback = models.Feedback.objects.filter(feedback_id=feedback_id).first()
        if not feedback:
            return response_dict.response_dict(1, "上传失败", "反馈不存在")
        feedback = models.FeedbackPhoto.objects.create(
            photo_type=photo_type,
            feedback=feedback,
            photo=image,
        )

        return response_dict.response_dict(0, "上传成功")
    except Exception as e:
        return response_dict.response_dict(1, "上传失败", str(e))


# 获取反馈列表
def get_feedback_list(request):
    if request.method != "GET":
        return response_dict.response_dict(1, "请求方法错误", "仅支持GET方法")
    try:
        feedback_type = request.GET.get("feedback_type")
        feedback_id = request.GET.get("feedback_id")
        status = request.GET.get("status")
        search = request.GET.get("search")
        # if feedback_type:
        #     feedbacks = models.Feedback.objects.filter(user=request.user, feedback_type=feedback_type)
        # else:
        #     feedbacks = models.Feedback.objects.filter(user=request.user)
        query = Q()
        if feedback_type:
            query &= Q(feedback_type=feedback_type)
        if feedback_id:
            query &= Q(feedback_id=feedback_id)
        if search:
            query &= Q(content__contains=search)
        if status:
            query &= Q(status=status)
        if request.user.role != 1:
            query &= Q(user=request.user)
        feedbacks = models.Feedback.objects.filter(query).order_by("-update_time")
        data = [utils_model_to_dict(feedback) for feedback in feedbacks]
        return response_dict.response_dict(0, "获取成功", data)
    except Exception as e:
        return response_dict.response_dict(1, "获取失败", str(e))


# 处理反馈
def handle_feedback(request):
    if request.method != "POST":
        return response_dict.response_dict(1, "请求方法错误", "仅支持POST方法")
    try:
        feedback_id = request.POST.get("feedback_id")
        result = request.POST.get("result")
        if not feedback_id:
            return response_dict.response_dict(1, "处理失败", "未包含反馈ID")
        feedback = models.Feedback.objects.filter(feedback_id=feedback_id).first()
        if not feedback:
            return response_dict.response_dict(1, "处理失败", "反馈不存在")
        feedback.status = 2
        feedback.result = result
        feedback.handler = request.user
        feedback.save()
        return response_dict.response_dict(0, "处理成功", utils_model_to_dict(feedback))
    except Exception as e:
        return response_dict.response_dict(1, "处理失败", str(e))


